<?php

namespace app\model\posts;

use app\model\user\User;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;

/**
 * 帖子评论表
 * Class PostComments
 * @package app\model\posts
 */
class PostComments extends BaseModel
{
    use ModelTrait;

    /**
     * 表明
     * @var string
     */
    protected $name = 'post_comments';
    protected $insert = ['add_time'];
    protected $autoWriteTimestamp = false;
    /**
     * 主键
     * @var string
     */
    protected $pk = 'id';

    protected function setAddTimeAttr($value)
    {
        return time();
    }

    public function user() {
        return $this->hasone(User::class, 'uid', 'user_id')->bind(['nickname']);
    }


    /**
     * 评论人id一对一关联
     * @date 2025/07/12
     * @return \think\model\relation\HasOne
     */
    public function userIdHasOne()
    {
        return $this->hasOne(\app\model\user\User::class, 'uid', 'user_id')->field('uid,nickname as user_name,avatar');
    }

    /**
     * 帖子id一对一关联
     * @date 2025/07/12
     * @return \think\model\relation\HasOne
     */
    public function postsIdHasOne()
    {
        return $this->hasOne(\app\model\posts\Posts::class, 'id', 'posts_id')->field('id,title');
    }

}